var utils = require("../../../utils/util.js");
var app = getApp();
var WxParse = require('../../../wxParse/wxParse.js');
Page({
  data: {
    pop:false,
    imgUrls:[],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    is_collected:0,
    sale_price:0,
    sales_amount:0,
    dominameImg:utils.dominameImg,
    abstracts: '',
    num:1,
    stock:1
  },
  onLoad: function (options){
    var that = this;
    wx.showLoading({
      title: "数据加载中",  
      mask: true  
    });
    wx.request({
      url: utils.dominame+'/api/zbds/goods/goodsInfo',
      data: {
        token:app.globalData.token,
        shop_goods_id:options.id
      },
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      complete: function (res) {
        setTimeout(() => {
          wx.hideLoading();
        }, 100); 
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var temp = WxParse.wxParse('article','html', res.data.data.shop_describe, that, 5);
          that.setData({ 
            imgUrls: res.data.data.pic.split(','),
            productName: res.data.data.shop_name,
            flag: false,
            article: temp,
            title: '商品详情',
            id: options.id,
            sales_amount: res.data.data.sales_amount,
            sale_price: res.data.data.sale_price,
            is_collected: res.data.data.is_collected,
            abstracts: res.data.data.abstracts,
            stock: res.data.data.stock,
            total: res.data.data.sale_price
          });
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:"none"
          });
        }
      }
    })
  },
  enterNum:function(e){
    var num;
    if (isNaN(e.detail.value) || e.detail.value.trim()==''){
      num=1;
    } else if (e.detail.value > this.data.stock){
      num = this.data.stock;
    }else{
      num = e.detail.value;
    };
    console.log(num);
    this.setData({
      num: num
    });
    this.cal();
  },
  popover:function(){
    this.setData({
      pop:true
    });
  },
  closePopover:function(){
    this.setData({
      pop: false
    });
  },
  stopProgration:function(e){
    e.stopProgration
  },
  buyNow:function(){
    var that=this;
    wx.navigateTo({
      url: '../shopMallSubmitOrder/SubmitOrder?id=' + that.data.id + '&counts=' + that.data.num,
      success:function(){
        that.closePopover();
      }
    })
  }, 
  goHome:function(){
    wx.navigateTo({
      url: '../shopMallHome/sp_home',
    })
  },
  collect:function(){
    let me = this;
    wx.showLoading({
      title: '加载中',
    });
    console.log(this.data.is_collected);
    if (this.data.is_collected==0){
      wx.request({
        url: utils.dominame + '/api/v1/goods/user_goods_collect',
        data: {
          token: app.globalData.token,
          shop_goods_id: me.data.id
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        complete: function (res) {},
        success: function (res) {
          console.log(res);
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
          if (res.data.code == 0) {
            me.setData({
              is_collected:1
            });
          }
        }
      })
    }else{
      wx.request({
        url: utils.dominame + '/api/v1/goods/deleteGoods_collect',
        data: {
          token: app.globalData.token,
          goods_id: me.data.id
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        complete: function (res) {
          // setTimeout(() => {
          //   wx.hideLoading();
          // }, 100);
        },
        success: function (res) {
          console.log(res);
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
          if (res.data.code == 0) {
            me.setData({
              is_collected:0
            });
          }
        }
      })
    };
  },
  plus:function(){
    let num=this.data.num;
    num++;
    this.setData({
      num:num
    });
    this.cal();
  },
  reduce:function(){
    let num = this.data.num;
    num--;
    this.setData({
      num: num
    });
    this.cal();
  },
  cal:function(){
    let total = this.data.num * this.data.sale_price;
    this.setData({
      total: total
    });
  },
  onShareAppMessage: function (res) {
    console.log(res)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.productName,
      path: '/pages/shoppingMall/shopMallProductDetail/productDetail?id='+this.data.id,
      imageUrl: utils.dominameImg + this.data.imgUrls[0],
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '分享成功',
          icon: 'none'
        });
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          icon: 'none'
        });
      }
    }
  }
})